<?php

namespace App\Logic;

use \Common\Logic\BaseLogic;

class WithdrawLogic extends BaseLogic
{
    private $withdrawalModel;

    public function __construct()
    {
        parent::__construct();
        $this->withdrawalModel = D('Withdrawal');
    }

    /**
     * 提交取现申请
     * @param $userId
     * @param $name
     * @param $income
     * @return mixed
     */
    public function applyWithdraw($userId, $name, $income)
    {
        $name = trim($name);
        if (empty($name)) {
            return static::$res->err("真实姓名不能为空");
        }
        $income = (int)$income;
        if (empty($income)) {
            return static::$res->err("提现金额不能为空");
        }
        if ($income < 300) {
            return static::$res->err("提现金额不能小于3元");
        }


        //获取用户余额
        $userLogic = A('App/User', 'Logic');
        $blanceInfoRes = $userLogic->getBlanceInfo($userId);
        if (static::$res->isErr($blanceInfoRes)) {
            return $blanceInfoRes;
        }
        $blanceInfo = $blanceInfoRes['Result'];
        $blance = $blanceInfo['blance'];
        if ($blance < $income) {
            return static::$res->err("余额不足");
        }


        $toDay = date('Y-m-d H:i:s');
        $where = array(
            "user_id" => $userId,
            "state" => array('neq', 3)
        );

        //判断日取现次数
        $dayWhere = array_merge($where, array(
            "add_time" => array(
                array("EGT", date('Y-m-d 00:00:00')),
                array("ELT", $toDay)
            )
        ));
        $dayCount = $this->withdrawalModel->where($dayWhere)->count();
        if ($dayCount >= 2) {
            return static::$res->err("一天不能取现超过两笔");
        }

        //判断周取现次数
        $weekWhere = array_merge($where, array(
            "add_time" => array(
                array("EGT", date('Y-m-d 00:00:00', strtotime("-7 day"))),
                array("ELT", $toDay)
            )
        ));
        $weekCount = $this->withdrawalModel->where($weekWhere)->count();
        if ($weekCount >= 5) {
            return static::$res->err("一周不能取现超过五笔");
        }

        //判断月取现次数
        $monthWhere = array_merge($where, array(
            "add_time" => array(
                array("EGT", date('Y-m-d 00:00:00', strtotime("-31 day"))),
                array("ELT", $toDay)
            )
        ));
        $monthCount = $this->withdrawalModel->where($monthWhere)->count();
        if ($monthCount >= 10) {
            return static::$res->err("一月不能取现超过十笔");
        }

        $data = array(
            "user_id" => $userId,
            "name" => $name,
            "sum" => $income,
            "add_time" => date('Y-m-d H:i:s'),
            "state" => 1
        );
        $id = $this->withdrawalModel->add($data);
        if (empty($id)) {
            return static::$res->err("数据库异常，提价失败");
        }

        return static::$res->suc("提价成功");
    }

    /**
     * 获取取现列表
     * @param int $page
     * @param int $size
     * @param string $userId
     * @return mixed
     */
    public function getApplyList($page = 1, $size = 20, $userId = '')
    {
        if (empty($userId)) {
            return static::$res->err("userId不能为空");
        }

        $where = array(
            "user_id" => $userId
        );

        $list = $this->withdrawalModel->where($where)->page($page . "," . $size)->order('id desc')->select();
        $count = $this->withdrawalModel->where($where)->count();

        return static::$res->suc("成功", array(
            "list" => $list,
            "count" => $count
        ));
    }

    /**
     * 获取一条取现记录明细
     * @param $userId
     * @param $id
     * @return mixed
     */
    public function getApplyDetial($userId, $id)
    {
        if (empty($userId)) {
            return static::$res->err("userId不能为空");
        }
        if (empty($id)) {
            return static::$res->err("记录ID不能为空");
        }
        $where = array(
            "user_id" => $userId,
            "id" => $id
        );

        $info = $this->withdrawalModel->where($where)->find();
        return static::$res->suc("成功", $info);
    }


}
